package com.sip.stream.utils.zfy.yuv;

import android.media.Image;

import com.sip.stream.utils.ImageYUVBean;

import java.nio.ByteBuffer;
import java.util.Arrays;
import kotlin.jvm.internal.ByteCompanionObject;


public class ZFYYUVMONOTool extends ICamera2ImageReader {
    private byte[] emptyUV;

    public ZFYYUVMONOTool(int width, int height) {
        super(width, height);
        this.emptyUV = null;
        byte[] bArr = new byte[(width * height) / 2];
        this.emptyUV = bArr;
        Arrays.fill(bArr, (byte) ByteCompanionObject.MIN_VALUE);
    }

    @Override
    public ImageYUVBean getDataFormImage(Image image) {
        byte[] obtainOutputData = obtainOutputData(this.dataLength);
        ByteBuffer buffer = image.getPlanes()[0].getBuffer();
        image.getPlanes()[1].getBuffer();
        ByteBuffer buffer2 = image.getPlanes()[2].getBuffer();
        try {
            buffer.get(obtainOutputData, 0, this.ySize);
            buffer2.get(obtainOutputData, this.ySize, buffer2.remaining());
            System.arraycopy(this.emptyUV, 0, obtainOutputData, this.width * this.height, this.emptyUV.length);
            ImageYUVBean imageYUVBean = new ImageYUVBean();
            imageYUVBean.setCameraData(obtainOutputData);
            imageYUVBean.setImageTimestampus(image.getTimestamp() / 1000);
            return imageYUVBean;
        } catch (Exception e) {
            e.printStackTrace();
            recycle(obtainOutputData);
            return null;
        }
    }
}
